package com.zt.rongda.stock.controller;

import com.zt.rongda.stock.model.PickMaterial;
import com.zt.rongda.stock.service.PickMaterialService;
import com.zt.servlet.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.DecimalFormat;

@Controller
@RequestMapping(value = "/pickMaterial")
public class PickMaterialController {

    @Autowired
    private PickMaterialService pickMaterialService;

    @RequestMapping(value = "/query")
    @ResponseBody
    
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(pickMaterialService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        pickMaterialService.add(getPickMaterial(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        pickMaterialService.update(getPickMaterial(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        pickMaterialService.delete(reqBean.getInfo("billId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private PickMaterial getPickMaterial(ReqBean reqBean) {
        PickMaterial pickMaterial = new PickMaterial();
        pickMaterial.setBillId(reqBean.getInfo("billId"));
        pickMaterial.setStockId(reqBean.getInfo("stockId"));
        pickMaterial.setStockName(reqBean.getInfo("stockName"));
        pickMaterial.setMaterialSpec(reqBean.getInfo("materialSpec"));
        pickMaterial.setMaterialUnit(reqBean.getInfo("materialUnit"));
        pickMaterial.setApplyCount(reqBean.getInfoInt("applyCount"));
        pickMaterial.setActualCount(reqBean.getInfoInt("actualCount"));
        BigDecimal b1 = new BigDecimal(Double.parseDouble(reqBean.getInfo("price")));
        DecimalFormat df = new DecimalFormat("######0.00");
        pickMaterial.setPrice(Double.parseDouble(df.format(b1)));
        pickMaterial.setTotalPrice(reqBean.getInfoDouble("totalPrice"));
        pickMaterial.setPickDeptId(reqBean.getInfo("deptId"));
        pickMaterial.setPickEmpId(reqBean.getInfo("pickEmpId"));
        pickMaterial.setStatus(reqBean.getInfo("status"));
        pickMaterial.setRegistrant(reqBean.getUserId());
        pickMaterial.setOperator(reqBean.getUserId());
        pickMaterial.setRemark(reqBean.getInfo("remark"));
        return pickMaterial;
    }

}